package org.invigorare.auctions.data.entities;

import java.util.*;

/**
 * A class containing data corresponding to 
 * one row of the License_table, and related helper methods.
 */
public class Licensee {
	public String name;
	public String addr_street;
	public String addr_state;
	public String addr_pin;
	
	public String seller_lic;
	public String buyer_lic;
	
	public String organization;
	
	public Licensee(String name, String addr_street, String addr_state, String addr_pin,
			String seller_lic, String buyer_lic, String organization) {
		this.name = name;
		this.addr_street = addr_street;
		this.addr_state = addr_state;
		this.addr_pin = addr_pin;
		
		this.seller_lic = seller_lic;
		this.buyer_lic = buyer_lic;
		
		this.organization = organization;
	}
	
	
	public enum LicenseType {SELLER_LICENSE, BUYER_LICENSE};
	public static Map<String, Licensee> makeMapFromList(List<Licensee> list, LicenseType keyByLicenseType) {
		HashMap<String,Licensee> map = new HashMap<String, Licensee>(list.size());
		
		for (Licensee lic : list) {
			String key;
			if (keyByLicenseType == LicenseType.SELLER_LICENSE) 
				key = lic.seller_lic;
			else key = lic.buyer_lic;
			
			if (key != null)
				map.put(key, lic);
		}
		
		return map;
	}
	
	public static List<Licensee> filterByLicenseType(List<Licensee> list, LicenseType licenseType) {
		List<Licensee> out = new ArrayList<Licensee>(list.size());
		
		for (Licensee lic : list) {
			if (licenseType == LicenseType.SELLER_LICENSE) {
				if (lic.seller_lic != null) {
					out.add(lic);
				}
			} else if (licenseType == LicenseType.BUYER_LICENSE) {
				if (lic.buyer_lic != null) {
					out.add(lic);
				}
			}
		}
		
		return out;
	}
	
	public static List<String> extractLicenseNumbersFromList(List<Licensee> list, LicenseType licenseType) {
		List<String> out = new ArrayList<String>(list.size());
		
		for (Licensee lic : list) {
			if (licenseType == LicenseType.SELLER_LICENSE) {
				out.add(lic.seller_lic);
			} else if (licenseType == LicenseType.BUYER_LICENSE) {
				out.add(lic.buyer_lic);
			}
		}
		
		return out;
	}
	
}
